package com.cema.manage.project.manage.versionUpdate.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.framework.web.domain.BaseEntity;
import com.cema.manage.project.manage.versionUpdate.domain.VersionUpdate;
import com.cema.manage.project.manage.versionUpdate.domain.VersionUpdateVo;
import com.cema.manage.project.manage.versionUpdate.service.IVersionUpdateService;
import com.cema.manage.project.system.dict.domain.DictData;
import com.cema.manage.project.system.dict.service.IDictDataService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cema.manage.framework.web.page.TableDataInfo;


/**
 * 版本更新 信息操作处理
 *
 * @author reasahi
 * @date 2024-07-12
 */
@Controller
@RequestMapping("/manage/versionUpdate")
public class VersionUpdateController extends BaseController {
    private String prefix = "manage/versionUpdate";

    @Autowired
    private IVersionUpdateService versionUpdateService;

    @Autowired
    private IDictDataService dictDataService;

    @GetMapping()
    @RequiresPermissions("manage:versionUpdate:view")
    public String versionUpdate() {
        return prefix + "/versionUpdate";
    }

    /**
     * 查询版本更新列表
     */
    @RequiresPermissions("manage:versionUpdate:list")
    @ResponseBody
    @GetMapping("/list")
    public TableDataInfo list(BaseEntity baseEntity) {
        startPage();
        EntityWrapper entityWrapper = new EntityWrapper();
        String value = baseEntity.getSearchValue();
        if (StrUtil.isNotEmpty(value)) {
            entityWrapper.eq("vu_version_name", baseEntity.getSearchValue());
        }
        List<DictData> dictDataList = dictDataService.selectDictDataByType("channel_package");
        List<VersionUpdateVo> versionUpdateVoList = new ArrayList<>();
        List<VersionUpdate> list = versionUpdateService.selectList(entityWrapper);
        for (VersionUpdate versionUpdate : list) {
            VersionUpdateVo versionUpdateVo = BeanUtil.toBean(versionUpdate, VersionUpdateVo.class);
            Optional<DictData> dictDataOptional = dictDataList.stream().filter(dictData -> dictData.getDictValue().equals(String.valueOf(versionUpdate.getVuChannelPackage()))).findFirst();
            versionUpdateVo.setVuChannelPackageName(dictDataOptional.get().getDictLabel());
            versionUpdateVoList.add(versionUpdateVo);
        }
        return getDataTable(versionUpdateVoList);
    }

    /**
     * 新增版本更新
     */
    @RequiresPermissions("manage:versionUpdate:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改版本更新
     */
    @RequiresPermissions("manage:versionUpdate:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        VersionUpdate versionUpdate = versionUpdateService.selectById(id);
        model.addAttribute("versionUpdate", versionUpdate);
        return prefix + "/edit";
    }

    /**
     * 保存版本更新
     */
    @RequiresPermissions("manage:versionUpdate:save")
    @PostMapping("/save")
    @ResponseBody
    public Message save(VersionUpdate versionUpdate) {
        Integer id = versionUpdate.getId();
        if (StringUtils.isNotNull(id)) {
            if (versionUpdateService.updateById(versionUpdate)) {
                return Message.success();
            }
            return Message.error();
        } else {
            if (versionUpdateService.insertVersion(versionUpdate)) {
                return Message.success();
            }
            return Message.error();
        }
    }

    /**
     * 删除版本更新
     */
    @RequiresPermissions("manage:versionUpdate:remove")
    @PostMapping("/remove/{id}")
    @ResponseBody
    public Message remove(@PathVariable("id") Integer id) {
        if (versionUpdateService.deleteById(id)) {
            return Message.success();
        }
        return Message.error();
    }

    /**
     * 批量删除版本更新
     */
    @RequiresPermissions("manage:versionUpdate:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Message remove(@RequestParam("ids[]") Integer[] ids) {
        List list = new ArrayList();
        for (Integer n : ids) {
            list.add(n);
        }
        if (versionUpdateService.deleteBatchIds(list)) {
            return Message.success();
        }
        return Message.error();
    }

}
